package com.estar.integaration.MQ;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.estar.integaration.Util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "mysql.queue",durable = "true",autoDelete = "false"),
                exchange = @Exchange(value = "mysql.exchange",type = "fanout",durable = "true",autoDelete = "false"),
                key = "canal.routing.key"
        )
)
public class SQLReceiver {
//    queues = "mysql.queue"
    Logger logger = LoggerFactory.getLogger(SQLReceiver.class);

    @RabbitHandler
    public void receiveMsg(byte[] result){
        String message = null;
        try {
            message = new String(result, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        if(StringUtils.isEmpty(message)){
            return;
        }
        JSONObject jsonObject = JSON.parseObject(message);
        if(jsonObject.get("database").equals("xxl_job")){
            return;
        }
        System.out.println("收到数据库的更新------>"+ jsonObject);

    }
}
